//邮件服务API
package com.example.mybatis.ApiInterface;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin//跨域问题
@RestController
public class EmailApi {
    @Autowired
    private JavaMailSender mailSender;

    //    获取发送方账户信息
    @Value("${spring.mail.username}")
    @GetMapping("/email")
//    谁要接收eamil,邮件标题title,邮件内容centent
    public boolean email(@RequestParam(value = "email", required = true) String email, @RequestParam(value = "title", required = true) String title, @RequestParam(value = "centent", required = true) String centent) {
//        获得SimpleMailMessage
        SimpleMailMessage message = new SimpleMailMessage();
//        发送方邮箱地址
        message.setFrom("875198099@qq.com");
//        接受方邮箱地址
        message.setTo(email);
//        接收方收件标题
        message.setSubject(title);
//        接收方邮件内容
        message.setText(centent);
//        try catch
        try {
            mailSender.send(message);
            //邮件发送成功返回true
            return true;
        } catch (MailException e) {
//            邮件发送失败返回false
            System.out.println("邮件发送错误" + e);
            return false;
        }
    }
}
